#!/bin/bash

list_dir=/dev/shm/test
tmplist=/tmp/list
tmpfile=/tmp/file
playlist=/tmp/pls

rand_playlist()
{
    find $list_dir/ -name "*.mp3" > /tmp/list

    line=$(wc -l $tmplist | awk '{print $1}')

    for i in $(seq 1 $line)
    do
        l=$(head -n $i $tmplist | tail -n 1)
        echo ${RANDOM}_$l
    done | sort -n > $tmpfile

    for i in $(seq 1 $line)
    do
        l=$(head -n $i $tmpfile | tail -n 1)
        echo ${l#*_}
    done > $playlist
}

[ x$1 = xrand -o ! -e "$playlist" ] && rand_playlist

mplayer -playlist $playlist
